//
//  YWGestureNavigationController2.m
//  YueshengToutiao
//
//  Created by yellow on 2021/1/7.
//  Copyright © 2021 YW. All rights reserved.
//

#import "YWGestureNavigationController2.h"

@interface YWGestureNavigationController2 ()


@property (nonatomic,strong)UIPanGestureRecognizer * panRecongizer;

@property (nonatomic, strong) NSMutableArray *images;

@property (nonatomic, strong) UIImageView *lastVcView;

@property (nonatomic, strong) UIView *cover;
@end

@implementation YWGestureNavigationController2

//网友2写的
#warning - 这个和网友1不同的是它的手势不是add在navVC上而是add在每个VC上 这个比网友1的退出动画会好一点，
#warning - 但是截图的效果还是一般，不应该是一进来就截图，因为这样数据都还没加载好，应该是准备跳转的时候才截图
#warning - 暂时思路是这样有需要再修改

- (UIImageView *)lastVcView

{

    if (!_lastVcView) {

        UIWindow *window = [UIApplication sharedApplication].keyWindow;

        UIImageView *lastVcView = [[UIImageView alloc] init];

        lastVcView.frame = window.bounds;

        self.lastVcView = lastVcView;

    }

    return _lastVcView;

}

 

- (UIView *)cover

{

    if (!_cover) {

        UIWindow *window = [UIApplication sharedApplication].keyWindow;

        UIView *cover = [[UIView alloc] init];

        cover.backgroundColor = [UIColor blackColor];

        cover.frame = window.bounds;

        cover.alpha = 0.5;

        self.cover = cover;

    }

    return _cover;

}

 

- (NSMutableArray *)images

{

    if (!_images) {

        self.images = [[NSMutableArray alloc] init];

    }

    return _images;

}

- (void)viewDidLoad {

    [super viewDidLoad];

 

}

 

-(void)panAction:(UIPanGestureRecognizer*)panRecongizer

{

    

    // 如果只有1个子控制器,停止拖拽

    if (self.viewControllers.count <= 1) return;

    

    // 在x方向上移动的距离

    CGFloat tx = [panRecongizer translationInView:self.view].x;

    if (tx < 0) return;

    

    if (panRecongizer.state == UIGestureRecognizerStateEnded || panRecongizer.state == UIGestureRecognizerStateCancelled) {

        // 决定pop还是还原

        CGFloat x = panRecongizer.view.frame.origin.x;

        if (x >= self.view.frame.size.width * 0.5) {

            [UIView animateWithDuration:0.25 animations:^{

                panRecongizer.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0);

            } completion:^(BOOL finished) {

                [self popViewControllerAnimated:NO];

                [self.lastVcView removeFromSuperview];

                [self.cover removeFromSuperview];

                panRecongizer.view.transform = CGAffineTransformIdentity;

                [self.images removeLastObject];

            }];

        } else {

            [UIView animateWithDuration:0.25 animations:^{

                panRecongizer.view.transform = CGAffineTransformIdentity;

            }];

        }

    } else {

        // 移动view

        panRecongizer.view.transform = CGAffineTransformMakeTranslation(tx, 0);

        

        UIWindow *window = [UIApplication sharedApplication].keyWindow;

        // 添加截图到最后面

        self.lastVcView.image = self.images[self.images.count - 2];

        [window insertSubview:self.lastVcView atIndex:0];

        [window insertSubview:self.cover aboveSubview:self.lastVcView];

    }

    

}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{
    
    //push的时候隐藏tabbar
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    
    [super pushViewController:viewController animated:animated];

    

    UIPanGestureRecognizer *panRecongizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

    

    [viewController.view addGestureRecognizer:panRecongizer];

    [self createScreenShot];

}



/**

 *  产生截图

 */

- (void)createScreenShot

{

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    [self.images addObject:image];

}

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

    if (self.images.count > 0) return;

    

    [self createScreenShot];

}

 

 
@end
